import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:freedom/const/app_content.dart';
import 'package:freedom/entity/restful.dart';
import 'package:freedom/page/mahjong/common/mahjong_context.dart';
import 'package:freedom/page/mahjong/mahjong_game.dart';
import 'package:freedom/page/mahjong/provider/mahjong_room_info.dart';
import 'package:freedom/util/api_router.dart';
import 'package:freedom/util/http.dart';
import 'package:freedom/widget/current_game_commons.dart';
import 'package:provider/provider.dart';

class HuCardDialog extends StatefulWidget {
  final MahjongIndexGame game;
  const HuCardDialog({super.key, required this.game});

  @override
  HuCardDialogState createState() => HuCardDialogState();
}

class HuCardDialogState extends State<HuCardDialog> {
  bool onHook = false;

  double dialogWidth = 400.w;
  double dialogHeight = 200.h;
  double cardWidth = 400.w / 10;
  double fontSize = 400.w / 30;

  @override
  void initState() {
    onHook = MahjongContext.currentUser?.onHook ?? false;
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Consumer<MahjongRoomInfoProvider>(
        builder: (buildContext, provider, child) {
      //provider.cardList
      List<int> userCards = provider.cardList;
      userCards.sort();
      //MahjongContext().getDiskAllCard();
      Map<int, int> diskCardSumInfo = MahjongContext().getDiskAllCard();
      //provider.showTips
      return provider.showTips
          ? Center(
              child: Container(
                height: dialogHeight,
                width: dialogWidth,
                decoration: BoxDecoration(
                  color: const Color.fromARGB(71, 255, 255, 255),
                  borderRadius: BorderRadius.circular(10),
                  boxShadow: [
                    BoxShadow(
                      color: const Color.fromARGB(146, 19, 18, 18)
                          .withOpacity(0.5),
                      spreadRadius: 5,
                      blurRadius: 7,
                      offset: const Offset(0, 3), // changes position of shadow
                    ),
                  ],
                ),
                child: Material(
                    color: Colors.transparent,
                    child: Column(
                      children: [
                        Container(
                          height: 30.h,
                          clipBehavior: Clip.hardEdge,
                          decoration: const BoxDecoration(
                            gradient: LinearGradient(
                              begin: Alignment.centerLeft,
                              end: Alignment.centerRight,
                              colors: [
                                Color.fromARGB(255, 105, 182, 245),
                                Color.fromARGB(255, 17, 142, 243)
                              ],
                            ),
                          ),
                          child: Stack(
                            fit: StackFit.expand,
                            children: [
                              Align(
                                  alignment: const Alignment(-0.95, 0),
                                  child: Icon(
                                    Icons.person_pin,
                                    size: 20.h,
                                    color: Colors.white,
                                  )),
                              Align(
                                  alignment: const Alignment(-0.7, 0),
                                  child: Text(
                                    "胡牌提示",
                                    style: TextStyle(
                                        height: 1.0,
                                        fontSize: 18.sp,
                                        color: Colors.white,
                                        fontWeight: FontWeight.w800,
                                        fontFamily: AppContent.fontHyqmd),
                                  )),
                              Align(
                                alignment: const Alignment(0.95, 0),
                                child: InkWell(
                                  child: Icon(
                                    Icons.close,
                                    size: 20.h,
                                    weight: 900,
                                    color: Colors.white,
                                  ),
                                  onTap: () {
                                    widget.game.setSelectUser(null);
                                    MahjongContext().reInitHuCardTips(false,
                                        userAllCards: []);
                                  },
                                ),
                              )
                            ],
                          ),
                        ),
                        Container(
                          height: 130.h,
                          width: double.infinity,
                          padding: EdgeInsets.only(left: 5.h, right: 5.h),
                          decoration: const BoxDecoration(
                              border: Border(
                                  bottom: BorderSide(
                                      color: Color.fromARGB(26, 255, 255, 255),
                                      width: 1.0))),
                          child: userCards.isNotEmpty
                              ? GridView(
                                  scrollDirection: Axis.vertical,
                                  gridDelegate:
                                      const SliverGridDelegateWithFixedCrossAxisCount(
                                    crossAxisCount: 9, // 列数
                                    mainAxisSpacing: 0.0, // 垂直间距
                                    crossAxisSpacing: 0.0, // 水平间距
                                    childAspectRatio: 0.65, // 子项宽高比
                                  ),
                                  children: userCards
                                      .map((cardCode) => Center(
                                            child: Column(
                                              children: [
                                                CardWidget.buildExportCardInfo(
                                                    cardCode, cardWidth,
                                                    marginTop: true),
                                                Text(
                                                  diskCardSumInfo[cardCode] ==
                                                          null
                                                      ? "4"
                                                      : "${4 - diskCardSumInfo[cardCode]!}",
                                                  style: TextStyle(
                                                      height: 1.0,
                                                      fontSize: fontSize,
                                                      color: Colors.white,
                                                      fontWeight:
                                                          FontWeight.w800,
                                                      fontFamily:
                                                          AppContent.fontHyqmd),
                                                )
                                              ],
                                            ),
                                          ))
                                      .toList(),
                                )
                              : Center(
                                  child: Text(
                                    '暂无胡牌信息',
                                    style: TextStyle(
                                        height: 1.0,
                                        fontSize: 18.sp,
                                        color: Colors.white,
                                        fontWeight: FontWeight.w800,
                                        fontFamily: AppContent.fontHyqmd),
                                  ),
                                ),
                        ),
                        Expanded(
                            child: Container(
                          width: double.infinity,
                          alignment: Alignment.centerLeft,
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.end,
                            children: [
                              Text(
                                "挂机: ",
                                style: TextStyle(
                                    height: 1.0,
                                    fontSize: 15.h,
                                    color: Color.fromARGB(255, 255, 255, 255),
                                    fontWeight: FontWeight.w800,
                                    fontFamily: AppContent.fontHyqmd),
                              ),
                              Switch(
                                  value: onHook,
                                  activeColor:
                                      Color.fromARGB(255, 37, 172, 235),
                                  onChanged: (selected) {
                                    Http.getInstance().request<Restful>(
                                        ApiRouter.userHook,
                                        type: RequestEnum.POST,
                                        showErrorMsg: true,
                                        data: {'hook': !onHook},
                                        success: (data) {
                                      onHook = !onHook;
                                      MahjongContext.currentUser?.onHook =
                                          onHook;
                                      setState(() {});
                                    });
                                  })
                            ],
                          ),
                        ))
                      ],
                    )),
              ),
            )
          : const SizedBox();
    });
  }
}
